﻿#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif
#include "common.h"
#include "dialog.h"
#include "ui_dialog.h"
#include "zmqClient.h"
#include "thread/simpleclientthread.h"
#include "thread/simpleserverthread.h"
#include "thread/portclientthread.h"
#include "thread/portserverthread.h"
#include "thread/queueclientthread.h"
#include "thread/queueserverthread.h"
#include "thread/subclientthread.h"
#include "thread/pubserverthread.h"
#include "thread/reqclientthread.h"
#include "thread/repserverthread.h"
#include <QMessageBox>



Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    // 绑定client radio button信号和槽函数
    connect(ui->c_simple,SIGNAL(clicked(bool)),
                this,SLOT(onClient_Clicked()));
    connect(ui->c_port,SIGNAL(clicked(bool)),
                this,SLOT(onClient_Clicked()));
    connect(ui->c_queue,SIGNAL(clicked(bool)),
                this,SLOT(onClient_Clicked()));
    connect(ui->c_sub,SIGNAL(clicked(bool)),
                this,SLOT(onClient_Clicked()));
    connect(ui->c_req,SIGNAL(clicked(bool)),
                this,SLOT(onClient_Clicked()));


    // 绑定server radio button信号和槽函数
    connect(ui->s_simple,SIGNAL(clicked(bool)),
                this,SLOT(onServer_Clicked()));
    connect(ui->s_port,SIGNAL(clicked(bool)),
                this,SLOT(onServer_Clicked()));
    connect(ui->s_queue,SIGNAL(clicked(bool)),
                this,SLOT(onServer_Clicked()));
    connect(ui->s_pub,SIGNAL(clicked(bool)),
                this,SLOT(onServer_Clicked()));
    connect(ui->s_rep,SIGNAL(clicked(bool)),
                this,SLOT(onServer_Clicked()));

    Dialog* dialog = this;

    connect(ui->startClient, &QPushButton::clicked, this, [=](){
        QList<QRadioButton*> list = ui->clientBox->findChildren<QRadioButton*>();
        for (int i = 0 ;i < list.size();i++) {
            QRadioButton *radio = (QRadioButton*)list.at(i);
            if (radio->isChecked()) {
                switch (i) {
                   case 0:{
                       QString str = "start simple client";
                       ui->message->appendPlainText(str);
                       qDebug() << str << endl;
                       SimpleClientThread* simpleC = new SimpleClientThread(this->ui->c_ip_txt->text(), this->ui->c_port_txt->text().toInt());
                       simpleC->start();
                       break;
                   }
                   case 1:{
                       QString str = "start port client";
                       ui->message->appendPlainText(str);
                       qDebug() << str << endl;
                       PortClientThread* portC = new PortClientThread(this->ui->c_ip_txt->text(), this->ui->c_port_txt->text().toInt());
                       portC->start();
                       break;
                   }
                   case 2: {
                        QString str = "start queue client";
                        ui->message->appendPlainText(str);
                        qDebug() << str << endl;
                        QueueClientThread* queueC = new QueueClientThread(this->ui->c_ip_txt->text(), this->ui->c_port_txt->text().toInt());
                        queueC->start();
                        break;
                   }
                   case 3: {
                       QString str = "start sub client";
                       ui->message->appendPlainText(str);
                       qDebug() << str << endl;
                       SubClientThread* subC = new SubClientThread(this->ui->c_ip_txt->text(), this->ui->c_port_txt->text().toInt());
                       subC->start();
                       break;
                   }
                   case 4: {
                        QString str = "start req client";
                        ui->message->appendPlainText(str);
                        qDebug() << str << endl;
                        ReqClientThread* reqC = new ReqClientThread(this->ui->c_ip_txt->text(), this->ui->c_port_txt->text().toInt());
                        reqC->start();
                        break;
                    }
                }
                 dialog->disabledClient();
                 return;
            }
        }
        QMessageBox::about(NULL,  "提示",  "请选择客户端类型进行启动!");


    });

    connect(ui->startServer, &QPushButton::clicked, this, [=](){
        QList<QRadioButton*> list = ui->serverBox->findChildren<QRadioButton*>();
        for (int i = 0 ;i < list.size();i++) {
            QRadioButton *radio = (QRadioButton*)list.at(i);
            if (radio->isChecked()) {
                switch (i) {
                    case 0: {
                        QString str = "start simple server";
                        ui->message->appendPlainText(str);
                        qDebug() << str << endl;
                        SimpleServerThread* simpleS = new SimpleServerThread(this->ui->s_ip_txt->text(), this->ui->s_port_txt->text().toInt());
                        simpleS->start();
                        break;
                    }
                    case 1: {
                        QString str = "start port server";
                        ui->message->appendPlainText(str);
                        qDebug() << str << endl;
                        PortServerThread* portS = new PortServerThread(this->ui->s_ip_txt->text(), this->ui->s_port_txt->text().toInt());
                        portS->start();
                        break;
                    }
                    case 2:{
                        QString str = "start queue server";
                        ui->message->appendPlainText(str);
                        qDebug() << str << endl;
                        QueueServerThread* queueS = new QueueServerThread(this->ui->s_ip_txt->text(), this->ui->s_port_txt->text().toInt());
                        queueS->start();
                        break;
                    }
                    case 3:{
                        QString str = "start sub server";
                        ui->message->appendPlainText(str);
                        qDebug() << str << endl;
                        PubServerThread* pubS = new PubServerThread(this->ui->s_ip_txt->text(), this->ui->s_port_txt->text().toInt());
                        pubS->start();
                        break;
                    }
                    case 4:{
                        QString str = "start rep server";
                        ui->message->appendPlainText(str);
                        qDebug() << str << endl;
                        RepServerThread* repS = new RepServerThread(this->ui->s_ip_txt->text(), this->ui->s_port_txt->text().toInt());
                        repS->start();
                        break;
                    }
                }
                dialog->disabledServer();
                return;
            }
        }
        QMessageBox::about(NULL,  "提示",  "请先选择服务端类型进行启动!");

    });

}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::onClient_Clicked()
{
//    QList<QRadioButton*> list = ui->clientBox->findChildren<QRadioButton*>();
//    for (int i = 0 ;i < list.size();i++) {
//        QRadioButton *radio = (QRadioButton*)list.at(i);
//        if (radio->isChecked()) {
//            switch (i) {
//                case 0:
//                   qDebug() <<"simple"<<endl;
//                   break;
//               case 1:
//                   qDebug() <<"port"<<endl;
//                   break;
//               case 2:
//                   qDebug() <<"queue"<<endl;
//                   break;
//               case 3:
//                   qDebug() <<"sub"<<endl;
//                   break;
//            }
//        }
//    }

//    // disabled other radio
//    ui->c_simple->setDisabled(true);
//    ui->c_port->setDisabled(true);
//    ui->c_queue->setDisabled(true);
//    ui->c_sub->setDisabled(true);

//    ui->s_simple->setDisabled(true);
//    ui->s_port->setDisabled(true);
//    ui->s_queue->setDisabled(true);
//    ui->s_pub->setDisabled(true);

}

void Dialog::onServer_Clicked()
{
//    QList<QRadioButton*> list = ui->serverBox->findChildren<QRadioButton*>();
//    for (int i = 0 ;i < list.size();i++) {
//        QRadioButton *radio = (QRadioButton*)list.at(i);
//        if (radio->isChecked()) {
//            switch (i) {
//                case 0:
//                   qDebug() <<"simple"<<endl;
//                   break;
//               case 1:
//                   qDebug() <<"port"<<endl;
//                   break;
//               case 2:
//                   qDebug() <<"queue"<<endl;
//                   break;
//               case 3:
//                   qDebug() <<"pub"<<endl;
//                   break;
//            }
//        }
//    }

//    // disabled other radio
//    ui->c_simple->setDisabled(true);
//    ui->c_port->setDisabled(true);
//    ui->c_queue->setDisabled(true);
//    ui->c_sub->setDisabled(true);

//    ui->s_simple->setDisabled(true);
//    ui->s_port->setDisabled(true);
//    ui->s_queue->setDisabled(true);
//    ui->s_pub->setDisabled(true);
}

void Dialog::disabledALl()
{
    ui->c_simple->setDisabled(true);
    ui->c_port->setDisabled(true);
    ui->c_queue->setDisabled(true);
    ui->c_sub->setDisabled(true);

    ui->s_simple->setDisabled(true);
    ui->s_port->setDisabled(true);
    ui->s_queue->setDisabled(true);
    ui->s_pub->setDisabled(true);

    ui->startClient->setDisabled(true);
    ui->startServer->setDisabled(true);
}

void Dialog::disabledClient()
{
    ui->c_simple->setDisabled(true);
    ui->c_port->setDisabled(true);
    ui->c_queue->setDisabled(true);
    ui->c_sub->setDisabled(true);
    ui->startClient->setDisabled(true);
}

void Dialog::disabledServer()
{
    ui->s_simple->setDisabled(true);
    ui->s_port->setDisabled(true);
    ui->s_queue->setDisabled(true);
    ui->s_pub->setDisabled(true);
    ui->startServer->setDisabled(true);
}
